package com.heima.search.controller;

import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.search.dtos.*;
import com.heima.search.service.ApUserSearchService;
import com.heima.search.service.AssonciateService;
import com.heima.search.service.SearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SearchController {

    @Autowired
    private ApUserSearchService searchService;

    @Autowired
    private SearchHistoryService historyService;

    /**
     * app用户的搜索
     *
     */
    @PostMapping("/api/v1/article/search/search")
    public ResponseResult<List<ArticleDto>> search(@RequestBody UserSearchDto dto){
        List<ArticleDto> list = searchService.search(dto);
        return ResponseResult.ok(list);
    }


    /**
     * 查询用户的搜索的历史记录
     *
     */
    @PostMapping("/api/v1/history/load")
    public ResponseResult<List<String>> load(@RequestBody SearchHistoryQueryDTO dto){
        List<String> list = historyService.load(dto);
        return ResponseResult.ok(list);
    }

    @PostMapping("/api/v1/history/del")
    public ResponseResult<Void> del(@RequestBody SearchHistoryDelDto dto){
         historyService.del(dto);
        return ResponseResult.ok();
    }

    @Autowired
    private AssonciateService assonciateService;

//    POST /api/v1/associate/search
    @PostMapping("/api/v1/associate/search")
    public ResponseResult<List<AssonciateResultDto>> searchKeyWords(
            @RequestBody AssonciateDto dto
    ){
        return assonciateService.searchKeyWords(dto);
    }
}
